/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import resourses.Constants;

/**
 *
 * @author Yash
 */
public class DBMS {

   // private static DataSource ds;
    private Connection conn;

//    static {
//        try {
//            InitialContext ctx = new InitialContext();
//            ds = (DataSource) ctx.lookup("jdbc/myDatasource");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    //jdbc/myDatasource

    private void estConnection() {
        try {
            Class.forName(Constants.dbms_driver);
            conn = DriverManager.getConnection(Constants.dbms_url, Constants.dbms_username, Constants.dbms_password);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        try {
//            conn = ds.getConnection();
//            if(conn!=null){
//                System.out.println("Connection Established !!!");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    private void closeConnection() {
        try {
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int guid(String sql) {
        int ru = 0;
        estConnection();
        
        try {
            Statement st = conn.createStatement();
            ru = st.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        closeConnection();
        return ru;
    }

    public ResultSet gselect(String sql) {
        ResultSet rs = null;
        estConnection();
        try {
            Statement st = conn.createStatement();
            rs = st.executeQuery(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;

    }
    
    public static void main(String[] a){
        DBMS d=new DBMS();
        d.estConnection();
                
    }
}
